﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GDFlacTool
{
    public static class ClientExtensions
    {
        private static EventHandler _event;

        // extension method on Control.ControlCollection 
        public static void SetEnterEvent(this Control.ControlCollection theCollection, EventHandler theEvent)
        {
            _event = theEvent;
            RecurseSetEnter(theCollection);
        }

        // recurse all the controls and add the Enter Event : 

        public static void RecurseSetEnter(Control.ControlCollection aCollection)
        {
            foreach (Control theControl in aCollection)
            {
                // "weed out" things like internal controls of the NumericUpDown Control 
                // String.IsNullOrWhiteSpace is FrameWork 4.0 
                // use Trim() followed by String.IsNullOrEmpty for FrameWork 3.5 
                if (!String.IsNullOrEmpty(theControl.Name.Trim()))
                {
                    //Console.WriteLine("setting enter handler for : " + theControl.Name + " : " + theControl.GetType().ToString());

                    theControl.Enter += _event;
                }

                if (theControl.Controls.Count > 0) 
                    RecurseSetEnter((Control.ControlCollection)theControl.Controls);
            }
        } 
    }
}
